<template>
    <div class="app-container">
        <!-- 查询条件开始 -->
        <el-form ref="queryForm" :model="queryParams" :inline="true" label-width="98px">
            <el-form-item label="检查项目名称" prop="checkItemName">
                <el-input
                        v-model="queryParams.checkItemName"
                        placeholder="请输入检查项目名称"
                        clearable
                        size="small"
                        style="width:240px"
                />
            </el-form-item>
            <el-form-item label="关键字" prop="keywords">
                <el-input
                        v-model="queryParams.keywords"
                        placeholder="请输入关键字"
                        clearable
                        size="small"
                        style="width:240px"
                />
            </el-form-item>
            <el-form-item label="项目类型" prop="typeId">
                <el-select
                        v-model="queryParams.typeId"
                        placeholder="项目类型"
                        clearable
                        size="small"
                        style="width:240px"
                >
                    <el-option
                            v-for="dict in typeOptions"
                            :key="dict.dictValue"
                            :label="dict.dictLabel"
                            :value="dict.dictValue"
                    />
                </el-select>
            </el-form-item>
            <el-form-item label="状态" prop="status">
                <el-select
                        v-model="queryParams.status"
                        placeholder="可用状态"
                        clearable
                        size="small"
                        style="width:240px"
                >
                    <el-option
                            v-for="dict in statusOptions"
                            :key="dict.dictValue"
                            :label="dict.dictLabel"
                            :value="dict.dictValue"
                    />
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="initCheckItem" >搜索</el-button>
            </el-form-item>
        </el-form>
        <!-- 查询条件结束 -->

        <!-- 表格工具按钮开始 -->
        <el-row :gutter="10" style="margin-bottom: 8px;">
            <el-col :span="1.5">
                <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="delLists" >删除</el-button>
            </el-col>
        </el-row>
        <!-- 表格工具按钮结束 -->

        <!-- 数据表格开始 -->
        <el-table v-loading="loading" border :data="checkItemTableList" @selection-change="handleSelectionChnage">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="检查项目ID" align="center" prop="checkItemId" />
            <el-table-column label="检查项目名称" align="center" prop="checkItemName" />
            <el-table-column label="关键字" align="center" prop="keywords" />
            <el-table-column label="项目单价" align="center" prop="unitPrice" />
            <el-table-column label="项目成本" align="center" prop="cost" />
            <el-table-column label="单位" align="center" prop="unit" />
            <el-table-column label="项目类别" prop="typeId" align="center" :formatter="typeIdFormatter" />
            <el-table-column label="状态" prop="status" align="center"  :formatter="statusFormat" />
            <el-table-column label="创建时间" align="center" prop="createTime" />
            <el-table-column label="操作" align="center">
                <template slot-scope="scope">
                    <el-button type="text" icon="el-icon-edit" size="mini" @click="handleUpdate(scope.row)">修改</el-button>
                    <el-button type="text" icon="el-icon-delete" size="mini"  @click="delPro(scope.row.checkItemId)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- 数据表格结束 -->
        <!-- 分页控件开始 -->
        <el-pagination
                v-show="total>0"
                :current-page="currentPage"
                :page-sizes="[5, 10, 20, 30]"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
        />
        <!-- 分页控件结束 -->

        <!-- 添加修改弹出层开始 -->
        <el-dialog
                :title="title"
                :visible.sync="open"
                width="600px"
                center
                append-to-body
        >
            <el-form ref="form" :model="form" :rules="rules" label-width="120px">
                <el-form-item label="项目类型" prop="typeId">
                    <el-radio-group v-model="form.typeId">
                        <el-radio
                                v-for="dict in typeOptions"
                                :key="dict.dictValue"
                                :label="dict.dictValue"
                                :value="dict.dictValue"
                        >{{ dict.dictLabel }}</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="检查项目名称" prop="checkItemName">
                    <el-input v-model="form.checkItemName" placeholder="请输入检查项目名称" clearable size="small" />
                </el-form-item>
                <el-form-item label="关键字" prop="keywords">
                    <el-input v-model="form.keywords" placeholder="请输入关键字" clearable size="small" />
                </el-form-item>
                <el-form-item label="检查项目价格" prop="unitPrice">
                    <el-input-number v-model="form.unitPrice" placeholder="请输入检查项目价格" clearable size="small" />
                </el-form-item>
                <el-form-item label="检查项目成本" prop="cost">
                    <el-input-number v-model="form.cost" placeholder="请输入检查项目成本" clearable size="small" />
                </el-form-item>
                <el-form-item label="单位" prop="unit">
                    <el-input v-model="form.unit" placeholder="请输入项目单位" clearable size="small" />
                </el-form-item>
                <el-form-item label="状态" prop="status">
                    <el-radio-group v-model="form.status">
                        <el-radio
                                v-for="dict in statusOptions"
                                :key="dict.dictValue"
                                :label="dict.dictValue"
                                :value="dict.dictValue"
                        >{{ dict.dictLabel }}</el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleSubmit">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </span>
        </el-dialog>
        <!-- 添加修改弹出层结束 -->

    </div>
</template>

<script>
    import qs from "qs";

    export default {
        name: "Examine",
        // 定义页面数据
        data() {
            return {
                // 是否启用遮罩层
                loading: false,
                // 选中数组
                ids: [],
                // 非单个禁用
                single: true,
                // 非多个禁用
                multiple: true,
                // 分页数据总条数
                total: 0,
                // 字典表格数据
                checkItemTableList: [],
                // 弹出层标题
                title: '',
                // 是否显示弹出层
                open: false,
                // 状态数据字典
                statusOptions: [],
                // 类型数据字典
                typeOptions: [],
                // 查询参数
                currentPage: 1,
                pageSize: 5,
                queryParams: {
                    checkItemName: undefined,
                    keywords: undefined,
                    typeId: undefined,
                    status: undefined
                },
                // 表单数据
                form: {},
                // 表单校验
                rules: {
                    checkItemName: [
                        { required: true, message: '检查项目名称不能为空', trigger: 'blur' }
                    ],
                    keywords: [
                        { required: true, message: '检查项目关键字不能为空', trigger: 'blur' }
                    ],
                    unitPrice: [
                        { required: true, message: '检查项目单价不能为空', trigger: 'blur' }
                    ],
                    cost: [
                        { required: true, message: '检查项目成本不能为空', trigger: 'blur' }
                    ],
                    unit: [
                        { required: true, message: '检查项目单位不能为空', trigger: 'blur' }
                    ]
                }
            }
        },
        created() {
            this.initCheckItem();
            this.initStatus();
            this.initTypeId();
        },
        methods:{

            handleUpdate(row){
                this.open=true;
                this.form=JSON.parse(JSON.stringify(row));
            },
            handleAdd(){
                this.open=true;
                this.form={};
            },
            handleSubmit(){
                this.$refs.form.validate((valid)=>{
                    if (valid){
                        this.$axios.post("/system/api/checkItems/saveAndUpdate",qs.stringify(this.form)).then(result=>{
                            if (result.data.data){
                                this.$message.success("添加成功")
                                this.open=false
                                this.form={}
                                this.initCheckItem();
                            }else {
                                this.$message.error("添加失败");
                            }
                        })
                    }else {
                        console.log('error submit!!')
                        return false
                    }
                })
            },


            // 取消
            cancel() {
                this.open = false
                this.title = ''
            },
            // 删除操作
            delPro(obj){
                this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.$axios.delete(`/system/api/checkItems/delById/${obj}`).then(result=>{
                        if (result.data.data){
                            this.initCheckItem();
                            this.$message({
                                type: 'success',
                                message: '删除成功!'
                            });
                        }else {
                            this.$message({
                                type: 'error',
                                message: '删除失败!'
                            });
                        }
                    })
                }).catch(() => {
                    this.$message.error({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            // 批量删除
            delLists(){
                this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.$axios.post("/system/api/checkItems/delListById",this.multipleSelection).then(result=>{
                        if (result.data.data){
                            this.initCheckItem();
                            this.$message({
                                type: 'success',
                                message: '删除成功!'
                            });
                        }else {
                            this.$message({
                                type: 'error',
                                message: '删除失败!'
                            });
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            initCheckItem(){
                this.$axios.post("/system/api/checkItems/searchCheckItem/"+this.currentPage+"/"+this.pageSize,this.queryParams,this.dateRange).then(result=>{
                    this.checkItemTableList=result.data.data.records;
                    this.total=result.data.data.total;
                })
            },

            // 数据表格的多选择框选择时触发
            handleSelectionChnage(selection) {
                this.multipleSelection=selection;
                this.single = selection.length !== 1
                this.multiple = !selection.length
            },
            //类型
            typeIdFormatter(row){
                let v=row.typeId;
                return this.formatDict(this.typeOptions, v)
            },
            //初始化类型
            initTypeId() {
                this.$axios.get("/system/api/dict/data/findByType/his_inspection_type").then(result => {
                    this.typeOptions = result.data.data;
                })
            },
            //状态解析
            statusFormat(row) {
                let v = row.status;
                return this.formatDict(this.statusOptions, v);
            },

            //初始化状态
            initStatus() {
                this.$axios.get("/system/api/dict/data/findByType/sys_normal_disable").then(result => {
                    this.statusOptions = result.data.data;
                })
            },

            // 分页pageSize变化时触发
            handleSizeChange(val) {
                this.pageSize = val
                // 重新查询
                this.initCheckItem();

            },
            // 点击上一页  下一页，跳转到哪一页面时触发
            handleCurrentChange(val) {
                this.currentPage = val
                // 重新查询
                this.initCheckItem();
            },
        }
    }
</script>

<style scoped>

</style>
